<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input class="box" :placeholder="placeholder" v-model="query" ref="query" />
    <i class="icon-dismiss" v-show="query" @click="clear"></i>
  </div>
</template>

<script>
  import {debounce} from 'common/js/util.js';

  export default {
    name: 'SearchBox',
    props: {
      placeholder: {
        type: String,
        default: '搜索歌曲、歌手'
      }
    },
    data() {
      return {
        query: ''
      }
    },
    methods: {
      clear() {
        this.query = ''
      },
      setQuery(newQuery) {
        this.query = newQuery
      },
      scrollBlur(){
        this.$refs.query.blur()
      }
    },
    created() {
      //wathc 写成这样子 可以写截流函数
      // 截流函数是为了搜索的时候减少每次输入文字的时候的请求数
      this.$watch('query', debounce((newQuery) => {
        this.$emit('query', newQuery)
      },200))
    },
  }

</script>

<style>


</style>
